# the minimum version of CMake.
cmake_minimum_required(VERSION 3.5.0)
project(image-knife-pro)

option(IMAGEKNIFEC_ENABLE_INSTALL "" ON)
option(ENABLE_PIXElMAP_GET_ALLOCATION_BYTE_COUNT OFF)
option(USING_FFRT_API "" ON)
option(USING_ON_AREA_CHANGE_EVENT ON)

if (ENABLE_PIXElMAP_GET_ALLOCATION_BYTE_COUNT)
    add_definitions(-DENABLE_PIXElMAP_GET_ALLOCATION_BYTE_COUNT)
endif()

if (USING_FFRT_API)
    add_definitions(-DIMAGEKNIFEC_USING_FFRT_API)
endif()

if (USING_ON_AREA_CHANGE_EVENT)
    add_definitions(-DIMAGEKNIFE_USING_ON_AREA_CHANGE_EVENT)
else()
    add_definitions(-DIMAGEKNIFE_EXECUTE_REQUEST_ON_NAPI_CREATE_NODE)
endif()

set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})

if(DEFINED PACKAGE_FIND_FILE)
    include(${PACKAGE_FIND_FILE})
endif()

# Specify the C++ standard to use
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

include_directories(${NATIVERENDER_ROOT_PATH}
                    ${NATIVERENDER_ROOT_PATH}/include
                    ${NATIVERENDER_ROOT_PATH}/include/data
                    ${NATIVERENDER_ROOT_PATH}/node
                    ${NATIVERENDER_ROOT_PATH}/cache
                    ${NATIVERENDER_ROOT_PATH}/interceptor
                    ${NATIVERENDER_ROOT_PATH}/thirdparty/lrucache11
                    ${NATIVERENDER_ROOT_PATH}/thirdparty/base64/include
                    ${NATIVERENDER_ROOT_PATH}/thirdparty/boundscheck/third_party_bounds_checking_function/include
                    ${NATIVERENDER_ROOT_PATH}/task
                    ${NATIVERENDER_ROOT_PATH}/utils
                    ${NATIVERENDER_ROOT_PATH}/data
                    ${NATIVERENDER_ROOT_PATH}/border
                    ${NATIVERENDER_ROOT_PATH}/key
                    ${NATIVERENDER_ROOT_PATH}/request
                    ${NATIVERENDER_ROOT_PATH}/transformation/gputransform
                    ${NATIVERENDER_ROOT_PATH}/transformation/gpufilter)

add_library(imageknifepro SHARED napi_init.cpp
                        imageknife_napi.cpp
                        imageknife_internal.cpp
                        imageknife_dispatcher.cpp
                        request/imageknife_request_internal.cpp
                        task/imageknife_task_internal.cpp
                        task/task_worker.cpp
                        task/task_worker_napi.cpp
                        interceptor/interceptor.cpp
                        cache/memory_cache.cpp
                        key/default_cache_key_generator.cpp
                        cache/file_cache.cpp
                        loader/imageknife_loader.cpp
                        loader/imageknife_loader_internal.cpp
                        node/imageknife_node_internal.cpp
                        node/imageknife_node_animator.cpp
                        option/imageknife_option_napi.cpp
                        option/animator_option_napi.cpp
                        data/image_data.cpp
                        data/image_source.cpp
                        data/onload_callback_napi.cpp
                        queue/default_job_queue.cpp
                        data/imageknife_data_napi.cpp
                        data/imageknife_colorfilter.cpp
                        border/border_option_napi.cpp
                        transformation/multi_transformation.cpp
                        transformation/transformation_factory.cpp
                        utils/imageknife_log_printer.cpp)

set(BASE64_WITH_NEON64 OFF CACHE BOOL "Disable NEON64 codepath in subproject")
add_subdirectory(${NATIVERENDER_ROOT_PATH}/thirdparty/base64)
add_subdirectory(${NATIVERENDER_ROOT_PATH}/thirdparty/boundscheck)

if(TARGET base64)
    # 设置C++编译选项
    target_compile_options(base64 PRIVATE
        $<$<COMPILE_LANGUAGE:CXX>:-Wno-unused-command-line-argument>
    )

    # 设置C编译选项
    target_compile_options(base64 PRIVATE
        $<$<COMPILE_LANGUAGE:C>:-Wno-unused-command-line-argument>
    )
endif()

# gpu transform
find_library (
            EGL-lib
            EGL )
find_library (
            GLES-lib
            GLESv3 )

target_sources(imageknifepro PRIVATE transformation/gputransform/gpu_transform.cpp)

target_link_libraries(imageknifepro PUBLIC libace_napi.z.so libace_ndk.z.so librcp_c.so libimage_packer.so
                             libimage_source.so libpixelmap.so librawfile.z.so libohresmgr.so libhilog_ndk.z.so
                             libohcrypto.so
                             libohfileuri.so
                             base64
                             boundscheck
                             libhitrace_ndk.z.so
                             libnative_drawing.so
                             libnative_display_manager.so
                             ${EGL-lib} ${GLES-lib} -ftrapv)
if (USING_FFRT_API)
    target_sources(imageknifepro PRIVATE task/task_worker_ffrt.cpp)
    target_link_libraries(imageknifepro PUBLIC libffrt.z.so)
endif()

if(CMAKE_BUILD_TYPE STREQUAL "Release")
    target_link_options(imageknifepro PUBLIC -s)
endif()

if("${OHOS_ARCH}" STREQUAL "arm64-v8a")
    target_link_directories(imageknifepro PUBLIC ${HMOS_SDK_NATIVE}/sysroot/usr/lib/aarch64-linux-ohos)
elseif("${OHOS_ARCH}" STREQUAL "armeabi-v7a")
    target_link_directories(imageknifepro PUBLIC ${HMOS_SDK_NATIVE}/sysroot/usr/lib/arm-linux-ohos)
elseif("${OHOS_ARCH}" STREQUAL "x86_64")
    target_link_directories(imageknifepro PUBLIC ${HMOS_SDK_NATIVE}/sysroot/usr/lib/x86_64-linux-ohos)
endif()

if (IMAGEKNIFEC_ENABLE_INSTALL)
set(IMAGEKNIFEC_INSTALL_PATH "${CMAKE_SOURCE_DIR}/../../../")
add_custom_command(TARGET imageknifepro POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/include ${IMAGEKNIFEC_INSTALL_PATH}/include)
endif()
